package com.example.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.blog.common.constant.Constants;
import com.example.blog.common.exception.BlogException;
import com.example.blog.common.util.BeanTransUtils;
import com.example.blog.common.util.JwtUtils;
import com.example.blog.common.util.Md5Utils;
import com.example.blog.mapper.UserInfoMapper;
import com.example.blog.pojo.dataobject.BlogInfo;
import com.example.blog.pojo.dataobject.UserInfo;
import com.example.blog.pojo.request.UserLoginRequest;
import com.example.blog.pojo.request.UserRegisterRequest;
import com.example.blog.pojo.response.UserInfoResponse;
import com.example.blog.pojo.response.UserLoginResponse;
import com.example.blog.service.BlogService;
import com.example.blog.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Resource(name = "userInfoMapper")
    private UserInfoMapper userInfoMapper;

    @Resource(name = "blogServiceImpl")
    private BlogService blogService;

    @Override
    public UserLoginResponse check(UserLoginRequest loginRequest) {
        //1.判断密码是否正确
        //2.如果正确，就返回
        UserInfo userInfo = selectUserInfoByName(loginRequest.getUserName());
        if (userInfo==null){
            //用户不存在
            throw new BlogException("用户不存在");
        }
//        if (!loginRequest.getPassword().equals(userInfo.getPassword())){
//            //密码不正确
//            throw new BlogException("密码不正确");
//        }

        if (!Md5Utils.verify(loginRequest.getPassword(),userInfo.getPassword())){
            //密码不正确
            throw new BlogException("密码不正确");
        }

        //密码正确
        Map<String ,Object> map = new HashMap<>();
        map.put("id",userInfo.getId());
        map.put("name",userInfo.getUserName());
        String token = JwtUtils.genJwt(map);
        return new UserLoginResponse(userInfo.getId(),token);
    }

    @Override
    public UserInfoResponse getUserInfo(Integer userId) {
        return BeanTransUtils.transUser(selectUserInfoById(userId));
    }

    @Override
    public UserInfoResponse getAuthorInfo(Integer blogId) {
        BlogInfo blogInfo = blogService.getBlogInfo(blogId);
        if (blogInfo==null){
            throw new BlogException("博客不存在");
        }

        return BeanTransUtils.transUser(selectUserInfoById(blogInfo.getUserId()));
    }

    //TODO
    @Override
    public Boolean add(UserRegisterRequest registerRequest) {
        //先判断数据库中有没有存在，有存在返回 已被使用
        UserInfo userInfo = selectUserInfoByName(registerRequest.getUserName());
        if (userInfo != null){
            throw new BlogException("用户已存在");
        }
        //不存在则正常添加
        try {
            UserInfo user = new UserInfo();
            BeanUtils.copyProperties(registerRequest,user);

            //对密码进行加密存储
            user.setPassword(Md5Utils.encrypt(user.getPassword()));
            userInfoMapper.insert(user);

            return true;
        }catch (Exception e){
            throw new BlogException("博客发布失败");
        }
    }

    public UserInfo selectUserInfoByName(String userName){
        return userInfoMapper.selectOne(
                new LambdaQueryWrapper<UserInfo>()
                        .eq(UserInfo::getUserName,userName)
                        .eq(UserInfo::getDeleteFlag, Constants.NOT_DELETE)
        );
    }

    public UserInfo selectUserInfoById(Integer userId){
        return userInfoMapper.selectOne(
                new LambdaQueryWrapper<UserInfo>()
                        .eq(UserInfo::getId,userId)
                        .eq(UserInfo::getDeleteFlag, Constants.NOT_DELETE)
        );
    }
}


































